const app = getApp();
var zhenzisms = require('../../utils/zhenzisms.js');
Page({
  data: {
    id: '',
    item: [],
    department: '',
    month: '',
    day: '',
    room: '',
    time: ''
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '编辑短信'
    })
    console.log(options.id)
    this.setData({
      id: options.id,
      department: app.globalData.department,
      item: app.globalData.list
    })
  },
  stap: function (e) {
    var that = this
    if (this.data.id == 0) {
      wx.redirectTo({
        url: '/pages/enroll/enroll',
      })
    }
    if (this.data.id == 1) {
      wx.redirectTo({
        url: '/pages/pass_2/pass_2',
      })
    }
    if (this.data.id == 2) {
      wx.redirectTo({
        url: '/pages/out_2/out_2',
      })
    }
    if (this.data.id == 2) {
      //更新app.js中的数据
      console.log(111)
      console.log(app.globalData.checked)
      for (var i = 0; i < app.globalData.sum; i++) {
        for (var j = 0; j < app.globalData.checked; j++) {
          if (app.globalData.data[i].id == app.globalData.list[j].id) {
            app.globalData.data[i].isUsed2 = 1
            console.log(app.globalData.data[i])
          }
        }
      }
      //更新知晓云数据
      for (var i = 0; i <app.globalData.checked; i++) {
        console.log(111)
        let tableName = 'data'
        let recordID = app.globalData.list[i].id // 数据行 id
         console.log(app.globalData.list[i])
        let Product = new wx.BaaS.TableObject(tableName)
        let product = Product.getWithoutData(recordID)
        product.set('isUsed2', 1)
        product.update().then(res => {
          console.log(res)
        }, err => {
          // err
        })
      }
      for (var i = 0; i < app.globalData.checked; i++) {
        zhenzisms.client.init("https://sms_developer.zhenzikj.com", 106780, "a3787380-10f6-4b86-918f-397d609ec130"
        );
        var params = {};
        params.number = String(that.data.item[i].phone);
        // params.number = String(17635862457);
        console.log(that.data.item[i].phone);
        console.log(that.data.item[i]);
        params.templateId = '1776';
        // var code = zhenzisms.client.createCode(4, 60, params.number); //生成验证码
        var templateParams = [that.data.item[i].nameValue, that.data.department];
        params.templateParams = templateParams;
        // console.log(code);
        zhenzisms.client.send(function (res) {
          wx.showToast({
            title: res.data.data,
            icon: 'none',
            duration: 2000
          })
        }, params);
      }
    }
    if (this.data.id == 1) {
        //更新app.js中的数据
    for (var i = 0; i < app.globalData.sum; i++) {
      for (var j = 0; j < app.globalData.checked; j++) {
        if (app.globalData.data[i].id == app.globalData.list[j].id) {
          app.globalData.data[i].isUsed2 = 1
        }
      }
    }
    //更新知晓云数据
    for (var i = 0; i < app.globalData.checked; i++) {
      let tableName = 'data'
      let recordID = app.globalData.list[i].id // 数据行 id

      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.getWithoutData(recordID)
      product.set('isUsed2', parseFloat(1))
      product.update().then(res => {
        console.log(res)
      }, err => {
        // err
      })
    }
      for (var i = 0; i < app.globalData.checked; i++) {
        zhenzisms.client.init("https://sms_developer.zhenzikj.com", 106780, "a3787380-10f6-4b86-918f-397d609ec130"
        );
        var params = {};
        params.number = String(that.data.item[i].phone);
        // params.number = String(17635862457);
        console.log(that.data.item);
        console.log(that.data.item[i].phone);
        params.templateId = '1801';
        // var code = zhenzisms.client.createCode(4, 60, params.number); //生成验证码
        var templateParams = [that.data.item[i].nameValue, that.data.department , this.data.qq];
        params.templateParams = templateParams;
        // console.log(code);
        zhenzisms.client.send(function (res) {
          wx.showToast({
            title: res.data.data,
            icon: 'none',
            duration: 2000
          })
        }, params);
      }
    }
    if (this.data.id == 0) {
        //更新app.js中的数据
    for (var i = 0; i < app.globalData.sum; i++) {
      for (var j = 0; j < app.globalData.checked; j++) {
        if (app.globalData.data[i].id == app.globalData.list[j].id) {
          app.globalData.data[i].isUsed1 = 1
        }
      }
    }

    //更新知晓云的数据
    for (var i = 0; i < app.globalData.checked; i++) {
      var id = String(app.globalData.list[i].id)
      let tableName = 'data'
      let recordID = id // 数据行 id

      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.getWithoutData(recordID)
      product.set('isUsed1', parseFloat(1))
      product.update().then(res => {
        console.log(res)
      }, err => {
        // err
      })
    }
      for (var i = 0; i < app.globalData.checked; i++) {
        zhenzisms.client.init("https://sms_developer.zhenzikj.com", 106780, "a3787380-10f6-4b86-918f-397d609ec130"
        );
        var params = {};
        params.number = String(that.data.item[i].phone);
        // params.number = String(17635862457);
        console.log(that.data.item[i].phone);
        params.templateId = '1777';
        // var code = zhenzisms.client.createCode(4, 60, params.number); //生成验证码
        var templateParams = [that.data.item[i].nameValue, String(that.data.department) , String(that.data.month) , String(that.data.day),String(that.data.time) , String(that.data.room) ];
        // var templateParams = [that.data.item[i].nameValue, String(that.data.department) , String(that.data.month) , String(that.data.day),String(that.data.time) , String(that.data.room) , String(that.data.phone)];
        params.templateParams = templateParams;
        // console.log(code);
        zhenzisms.client.send(function (res) {
          console.log(res)
          wx.showToast({
            title: res.data.data,
            icon: 'none',
            duration: 2000
          })
        }, params);
      }
    }
  },

  month_value: function (e) {
    console.log(e)
    this.setData({
      month: e.detail.value
    })
  },

  day_value: function (e) {
    this.setData({
      day: e.detail.value
    })
    console.log(e.detail.value)
  },

  room_value: function (e) {
    this.setData({
      room: e.detail.value
    })
    console.log(e.detail.value)
  },

  phone_value: function (e) {
    this.setData({
      phone: e.detail.value
    })
    console.log(e.detail.value)
  },

  time_value: function (e) {
    this.setData({
      time: e.detail.value
    })
    console.log(e.detail.value)
  },
  qq_value:function(e){
    this.setData({
      qq: e.detail.value
    })
    console.log(this.data.qq)
  }
  
})